home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / COMMON / FORMATS.H < prev    next >
C/C++ Source or Header  |  1994-09-24  |  9KB  |  309 lines

  1. /************************************************************************/
  2. /* This header    contains classes for image formats handling        */
  3. /* and some associated useful procedure                    */
  4. /* supported formats BMP,TGA,GIF,PCX                    */
  5. /*                                    */
  6. /*  Kiselev Y. &&  E.Podvoysky from ^Z for WROX press book, 1994    */
  7. /************************************************************************/
  8.  
  9. #ifndef FORMATS_H
  10. #define FORMATS_H
  11.  
  12. #include "grfile.h"
  13. #include "graph.h"
  14.  
  15. //----------------------------------- TARGA ----------------------------
  16.  
  17. typedef struct {    // targa file header
  18.     BYTE ID_field_length;
  19.     char color_map_type;
  20.     BYTE image_type;
  21.  
  22.     WORD first_colmap_entry;
  23.     WORD colmap_length;
  24.     char colmap_entry_size;
  25.  
  26.     int x;
  27.     int y;
  28.     int width;
  29.     int height;
  30.     char bitsperpixel;
  31.     BYTE attrbits : 4;
  32.     BYTE right_to_left : 1;
  33.     BYTE top_to_bottom : 1;
  34.     BYTE interleave : 2;
  35.     } targa_header;
  36.  
  37. typedef colortype targa_palette[256];
  38.  
  39. class targa_file: public graph_file_reader { // TARGA file reader
  40. private:
  41.     targa_header header;
  42.     int bytesperpixel,counter;
  43.     BOOL rle_comp;
  44. public:
  45.     targa_file(char *fname);     // open file, read headers and palette
  46.                     // allocates source_line
  47.     virtual ~targa_file();
  48.     virtual int get_next_line();    // returns -1 if file is finished
  49. };
  50.  
  51. class targa_file_saver: public graph_file_saver { // TARGA file saver
  52. private:
  53.     targa_header header;
  54.     int bytesperpixel,counter,count_row;
  55.     BOOL rle_comp;
  56.     int *arr_ofs;
  57. public:
  58.     targa_file_saver(char *fname, image_type dest_type_,
  59.             image_extention_type dest_ext_type_,
  60.             int width_,int height_,BGRpalette* pal,BOOL compress,
  61.             BOOL top_to_bottom_,BOOL left_to_right_);
  62.     virtual void update_palette(BGRpalette *pal);
  63.     virtual int put_next_line(BYTE* line);    // returns -1 if file is finished
  64.     ~targa_file_saver();
  65. };
  66.  
  67. //----------------------------------- BMP ------------------------------
  68.  
  69. const WORD BMPSIGNATURE = 'M'*256+'B';
  70.  
  71. typedef struct {
  72.     WORD bf_type;         //signature
  73.     long bf_size;          //size of file in DWORD
  74.     WORD bf_reserved1, bf_reserved2;
  75.     long bf_offbits;    //image start offset in byte
  76. } bmp_file_header;
  77.  
  78. typedef struct {
  79.     long bi_size;
  80.     long bi_width;
  81.     long bi_height;
  82.     WORD bi_planes;
  83.     WORD bi_bit_count;
  84.     long bi_compression;
  85.     long bi_size_image;
  86.     long bi_xpels_per_meter;
  87.     long bi_ypels_per_meter;
  88.     long bi_clr_used;
  89.     long bi_clr_important;
  90. } bmp_file_info_header;
  91.  
  92.  
  93. class bmp_file: public graph_file_reader  { // bitmap file reader
  94. private:
  95.     bmp_file_header header;
  96.     bmp_file_info_header info;
  97. public:
  98.     bmp_file(char *fname); //open file, check signature, read headers and palette
  99.                    //allocates source_line
  100.     virtual ~bmp_file();
  101.     virtual int get_next_line();    //returns -1 if file is finished
  102. };
  103.  
  104.  
  105. void build_bmp_info(bmp_file_info_header &info,
  106.         image_type f_type,    long width,long height);
  107.  
  108.  
  109. class bmp_file_saver: public  graph_file_saver{
  110.     bmp_file_info_header info;
  111.     void init(bmp_file_info_header *info_);
  112.     // is called from constructors
  113. public:
  114.     bmp_file_saver(char *fname,image_type dest_type_,
  115.              BGRpalette *palette, bmp_file_info_header *info_);
  116.     bmp_file_saver(char *fname,image_type dest_type_,
  117.             int width_,int height_,BGRpalette * pal);
  118.     virtual void update_palette(BGRpalette *pal);
  119.     virtual int put_next_line(BYTE *line);    //returns -1 if some error occurs
  120. };
  121.  
  122. //----------------------------------- GIF ------------------------------
  123.  
  124. #define LZW_BITS    12
  125. #define LZW_TABLE_SIZE    0x1000    // 1 << LZW_BITS
  126. #define HASH_SIZE    5003
  127.  
  128. const char GIFSIGNATURE[3] = "GIF";
  129.  
  130. typedef struct {
  131.     char signature[3];        // = "GIF"
  132.     char version[3];        // = "87a" or "89a" (version)
  133.     } gif_header;
  134.  
  135. typedef struct {
  136.     int width;
  137.     int height;
  138.     BYTE sizeofGCT : 3;        // size of Global Color Table
  139.     BYTE sortflag : 1;        // sort flag
  140.     BYTE colorres : 3;        // color resolution
  141.     BYTE GCTflag : 1;        // Global Color Table flag
  142.     BYTE backcolor;            // background color
  143.     BYTE pixelaspectratio;        // pixel aspect ratio
  144.     } logical_screen_descriptor;
  145.  
  146. #define IMAGE_DESCRIPTOR 0x2C
  147.  
  148. typedef struct {
  149.     int x;                // image left position
  150.     int y;                // image top position
  151.     int width;
  152.     int height;
  153.     BYTE sizeofLCT : 3;
  154.     BYTE reserved : 2;
  155.     BYTE sortflag : 1;        // sort flag
  156.     BYTE interlaceflag : 1;        // interlace flag
  157.     BYTE LCTflag : 1;        // Local Color Table flag
  158.     } image_descriptor;
  159.  
  160. class gif_file: public graph_file_reader { // GIF file reader
  161. private:
  162.     gif_header header;
  163.     logical_screen_descriptor scrdesc;
  164.     image_descriptor imagedesc;
  165.     WORD *prefixcode;
  166.     BYTE *secondbyte;
  167.     BYTE *decodestack;
  168.     BYTE LZW_code_size;
  169.     int num_bits,clear_code,end_of_info,first_code,max_code;
  170.  
  171.     DWORD bit_buffer;
  172.     int bit_count,counter;
  173.     BYTE blocksize,pixelvalue;
  174.     BOOL clear_flag;
  175.     WORD next_code,new_code,old_code;
  176.     BYTE *stack_top;
  177.  
  178.     WORD getcode();
  179.     BYTE *decode(BYTE *stack_bottom, WORD code);
  180.     // gif uses papa's buffer in specifical way, so
  181.     // it allocates and disposes it in constructors and destructors
  182. public:
  183.     gif_file(char *fname);         // open file, check signature, read headers and palette
  184.                     // allocates source_line
  185.     virtual ~gif_file();
  186.     virtual void seek_start();     // seek to image itself start (bottom line)
  187.     virtual int get_next_line();    // returns -1 if file is finished
  188. };
  189.  
  190. class gif_file_saver: public graph_file_saver { // GIF file saver
  191. private:
  192.     gif_header header;
  193.     logical_screen_descriptor scrdesc;
  194.     image_descriptor imagedesc;
  195.  
  196.     int *code_value;
  197.     WORD *prefixcode;
  198.     BYTE *secondbyte;
  199.     BYTE LZW_code_size;
  200.     int num_bits,clear_code,end_of_info,first_code,max_code;
  201.  
  202.     DWORD bit_buffer;
  203.     int bit_count;
  204.     WORD next_code,code;
  205.     BOOL first_byte;
  206.  
  207.     void putcode(WORD code);
  208.     int find_match(int hash_prefix,WORD hash_symbol);
  209.     // gif uses papa's buffer in specifical way, so
  210.     // it allocates and disposes it in constructors and destructors
  211. public:
  212.     gif_file_saver(char *fname, image_type dest_type_,
  213.             int width_,int height_,BGRpalette* pal);
  214.                 // open file, write signature, write headers and palette
  215.     virtual void update_palette(BGRpalette *pal);
  216.     virtual ~gif_file_saver();
  217.     virtual int put_next_line(BYTE *line);
  218. };
  219.  
  220.  
  221.  
  222. const char PCXSIGNATURE = 0x0A;
  223.  
  224. typedef struct {
  225.     char signature;
  226.     char version;
  227.     char encoding;
  228.     char bitsperpixel;
  229.     int x1;
  230.     int y1;
  231.     int x2;
  232.     int y2;
  233.     int Xres,Yres;
  234.     colortype palette[16];
  235.     char reserved;
  236.     char planes;
  237.     int bytesperline;
  238.     int palinterp;
  239.     int videoscrsizex;
  240.     int videoscrsizey;
  241.     char res[54];
  242.     } pcx_header;
  243.  
  244. class pcx_file: public graph_file_reader { // PCX file reader
  245. private:
  246.     pcx_header header;
  247.     BYTE prev_c,rep,datab;
  248.     int real_len;
  249. public:
  250.     pcx_file(char *fname);         // open file, check signature, read headers and palette
  251.                     // allocates source_line
  252.     virtual ~pcx_file();
  253.     virtual void seek_start();     // seek to image itself start (bottom line)
  254.     virtual int get_next_line();    // returns -1 if file is finished
  255.     long image_size();        // returns estimation of memory required to store image
  256. };
  257.  
  258. class pcx_file_saver: public graph_file_saver { // PCX file saver
  259. private:
  260.     pcx_header header;
  261. public:
  262.     pcx_file_saver(char *fname, image_type dest_type_,
  263.             image_extention_type dest_ext_type_,
  264.             int width_,int height_,BGRpalette* pal);    // open file, write headers
  265.                     // allocates source_line
  266.     virtual void update_palette(BGRpalette *pal);
  267.     virtual int put_next_line(BYTE *line);
  268. };
  269.  
  270. /*------------------*/
  271. /* any file opening */
  272.  
  273. graph_file_reader *open_image_file(char *fname);
  274. // if error occured returns null
  275. // and sets format_file_error in non-zero value
  276.  
  277. graph_file_saver *open_saver_file(char *fname,
  278.         image_type dest_type,
  279.         image_extention_type dest_ext_type,
  280.         int width, int height,
  281.         BGRpalette* palptr,
  282.         BOOL top_to_bottom, // if possible
  283.         BOOL left_to_right);
  284. // if error occured returns null
  285. // and sets format_file_error in non-zero value
  286.  
  287. extern int format_file_error;
  288. // 0 - Ok
  289. // 1 - File not found
  290. // 2 - Unknown file format
  291. // 3 - Format header error
  292. // 4 - Disk read / write error
  293.  
  294. char *format_file_error_report();
  295.  
  296. void choosevideomode(graph_file_reader *grf_r);
  297.     // set optimal (but 256 color) mode for image
  298.  
  299. void setpalette_by_BGR(BGRpalette pal);
  300.     // actually sets palette with values pal[i].x/4
  301.  
  302. // init simple standard palette for 256 colors mode
  303. void stand_palette(VGApalette &pal);
  304.  
  305. // simplest way to preview truecolor
  306. void put_TC_row(BYTE *rp, BYTE *gp, BYTE *bp,int x,int y,int xcount);
  307.  
  308. #endif
  309.